package com.idp.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.idp.common.base.BaseController;
import com.idp.common.error.SystemException;
import com.idp.common.model.PageModel;
import com.idp.common.model.ResultModel;
import com.idp.common.utils.ContextUtil;
import com.idp.common.utils.IOResUtil;
import com.idp.system.dto.SysUserDto;
import com.idp.system.dto.SysUserSearchDto;
import com.idp.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 用户Controller
 * @author king
 */
@Api(tags="用户")
@RestController
@RequestMapping("/user/")
public class SysUserController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(SysUserController.class);

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 分页查询
	 * @param sysUserSearchDto
	 * @return
	 */
	@ApiOperation("分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", paramType = "query", required = true, defaultValue = "1"),
			@ApiImplicitParam(name = "rows", value = "每页行数", paramType = "query", required = true, defaultValue = "10")
	})
	@PostMapping("findByPage")
	public ResultModel<PageModel<SysUserDto>> findByPage(@RequestBody SysUserSearchDto sysUserSearchDto) {
		return ResultModel.success(sysUserService.findByPage(sysUserSearchDto));
	}

	/**
	 * 通过id查询
	 * @param sysUserDto
	 * @return
	 */
	@ApiOperation("通过id查询")
	@ApiOperationSupport(includeParameters = {"sysUserDto.id"})
	@PostMapping("getById")
	public ResultModel<SysUserDto> getById(@RequestBody SysUserDto sysUserDto) {
		return ResultModel.success(sysUserService.getById(sysUserDto.getId()));
	}

	/**
	 * 新增
	 * @param sysUserDto
	 * @return
	 */
	@ApiOperation("新增")
	@ApiOperationSupport(ignoreParameters = {"sysUserDto.id", "sysUserDto.pwd","sysUserDto.roleNames"})
	@PostMapping("add")
	public ResultModel add(@RequestBody SysUserDto sysUserDto) {
		sysUserService.add(sysUserDto);
		return ResultModel.success("新增成功", null);
	}

	/**
	 * 修改
	 * @param sysUserDto
	 * @return
	 */
	@ApiOperation("修改")
	@ApiOperationSupport(ignoreParameters = {"sysUserDto.pwd", "sysUserDto.roleNames"})
	@PostMapping("update")
	public ResultModel update(@RequestBody SysUserDto sysUserDto) {
		sysUserService.update(sysUserDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 删除
	 * @param sysUserDto
	 * @return
	 */
	@ApiOperation("删除")
	@ApiOperationSupport(includeParameters = {"sysUserDto.id"})
	@PostMapping("delete")
	public ResultModel delete(@RequestBody SysUserDto sysUserDto) {
		sysUserService.delete(sysUserDto.getId());
		return ResultModel.success("删除成功", null);
	}

	/**
	 * 重置密码
	 * @param sysUserDto
	 * @return
	 */
	@ApiOperation("重置密码")
	@ApiOperationSupport(includeParameters = {"sysUserDto.id"})
	@PostMapping("resetPwd")
	public ResultModel resetPwd(@RequestBody SysUserDto sysUserDto) {
		sysUserService.resetPwd(sysUserDto.getId());
		return ResultModel.success("密码重置成功", null);
	}

	/**
	 * 修改密码
	 * @param updatePwdParams
	 * @return
	 */
	@ApiOperation("修改密码")
	@DynamicParameters(name = "updatePwdParams", properties = {
			@DynamicParameter(name = "oldPwd", value = "旧密码", dataTypeClass = String.class),
			@DynamicParameter(name = "newPwd", value = "新密码")
	})
	@PostMapping("updatePwd")
	public ResultModel updatePwd(@RequestBody Map<String, String> updatePwdParams) {
		String userId = ContextUtil.getLoginUserId();
		String oldPwd = updatePwdParams.get("oldPwd");
		String newPwd = updatePwdParams.get("newPwd");
		sysUserService.updatePwd(userId, oldPwd, newPwd);
		return ResultModel.success("密码修改成功", null);
	}

	/**
	 * 查询有效的用户
	 * @return
	 */
	@ApiOperation("查询有效的用户")
	@GetMapping("findValidUsers")
	public ResultModel<List<SysUserDto>> findValidUsers() {
		return ResultModel.success(sysUserService.findValidUsers());
	}

	/**
	 * 用户信息导出
	 * @param sysUserSearchDto
	 * @param response
	 * @return
	 */
	@ApiOperation("用户信息导出")
	@PostMapping("exportExcel")
	public ResultModel exportExcel(@RequestBody SysUserSearchDto sysUserSearchDto, HttpServletResponse response) {
		sysUserService.exportExcel(sysUserSearchDto, response);
		return ResultModel.success();
	}

	/**
	 * 下载模板
	 */
	@ApiOperation("下载模板")
	@PostMapping("downloadTemplate")
	public void downloadTemplate(HttpServletResponse response) {
		try {
			ClassPathResource classPathResource =  new ClassPathResource("excel/user_template.xls");
			String fileName = "用户导入模板.xls";
			IOResUtil.toResponse(classPathResource.getInputStream(), fileName, response);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new SystemException("模板下载失败");
		}
	}

	/**
	 * 用户信息导入
	 * @param file
	 * @return
	 */
	@ApiOperation("用户信息导入")
	@PostMapping("importExcel")
	public ResultModel importExcel(MultipartFile file) {
		sysUserService.importExcel(file);
		return ResultModel.success("导入成功", null);
	}
}
